﻿using Microsoft.AspNetCore.Idenmpotency;

namespace AspNetCore.Idenmptency.Test.Idenmptency
{
    public class AppIdenmptencyRequestHandler : IAspIdenmptencyRequestHandler
    {
        public async Task FirstRequestHandleAsync(IAspIdenmpotencyRequest request)
        {
            await request.Next(request.HttpContext);
        }
        public Task<bool> PreCheckHandlerAsync(IAspIdenmpotencyRequest request)
        {
            if(!string.IsNullOrWhiteSpace(request.Key))
            {
                return Task.FromResult<bool>(true);
            }
            else
            {
                request.HttpContext.Response.WriteAsJsonAsync(new
                {
                    Type= "Idenmpotency",
                    Result= "RequestKeyNotSetKey",
                });
                return Task.FromResult<bool>(false);
            }
        }
        public async Task ReiteratedRequestHandleAsync(IAspIdenmpotencyRequest request)
        {
            await request.HttpContext.Response.WriteAsJsonAsync(new
            {
                Type = "Idenmpotency",
                Result = "RequestIsReiterated",
            });
        }
    }
}
